package com.suian.util;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;

public class FileUtil2 {
    public static void main(String[] args) throws IOException {
        String basicBath = "D:\\suian\\javacode\\springcloud\\spring-cloud-demo\\product-service\\src\\main\\resources\\source.txt";
        String targetPath = "D:\\suian\\javacode\\springcloud\\spring-cloud-demo\\product-service\\src\\main\\resources\\target.txt";
        //原始数据
        ArrayList<ArrayList<Integer>> basicData = new ArrayList<>();
        //最终处理后的数据
        ArrayList<ArrayList<Integer>> targetData = new ArrayList<>();
        //读取文件
        loadFile(basicData, basicBath);

        System.out.println("读取文件后的数据-->");
        System.out.println(basicData);
        //处理数据，累加、排序、去重
        process(basicData,targetData);
        //报错文件
        saveFile(targetData, targetPath);

    }

    private static void saveFile(ArrayList<ArrayList<Integer>> targetData, String targetPath) throws IOException {
        BufferedWriter bw = null;
        try {
             bw = new BufferedWriter(new FileWriter(targetPath));
            for (ArrayList<Integer> line : targetData) {
                //每一行数据打印
                for (Integer num : line) {
                    bw.write(num+" ");
                }
                bw.newLine();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            assert bw != null;
            bw.close();
        }
    }

    private static void process(ArrayList<ArrayList<Integer>> res, ArrayList<ArrayList<Integer>> targetData) {

        //1、利用hashSet去重
        for (ArrayList<Integer> line : res) {
            HashSet<Integer> _line = new HashSet<>(line);
            ArrayList<Integer> arrayList = new ArrayList<>(_line);
            targetData.add(arrayList);
        }
        System.out.println("去重后的数据-->");
        System.out.println(targetData);
        //2、排序
        for (int i = 0; i < targetData.size(); i++) {
            if (i % 2 == 1) {
                //奇数升序
                Collections.sort(targetData.get(i), Collections.reverseOrder());
            } else {
                //偶数行降序
                Collections.sort(targetData.get(i));
            }
        }
        System.out.println("排序后的数据-->");
        System.out.println(targetData);
        //累加求和放在最后一位
        for (ArrayList<Integer> arrayList : targetData) {
            int sum = 0;
            for (Integer i : arrayList) {
                sum += i;
            }
            arrayList.add(sum);
        }
        System.out.println("累加求和后的数据-->");
        System.out.println(targetData);
    }

    private static void loadFile(ArrayList<ArrayList<Integer>> basicData, String path) throws IOException {
        FileReader fileReader = new FileReader(path);
        BufferedReader br = null;
        try {
             br = new BufferedReader(fileReader);
            String line;
            while (null != (line = br.readLine())) {
                ArrayList<Integer> arrayList = new ArrayList<>();
                //读取每一行文件
                System.out.println(line);
                String[] buffer = line.split(" ");
                for (String s : buffer) {
                    arrayList.add(Integer.parseInt(s));
                }
                basicData.add(arrayList);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            br.close();
        }
    }
}
